package sg.com.techstudio.cfoconnect.validator;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import sg.com.techstudio.cfoconnect.obj.RegisterObj;

@Component
public class RegisterObjValidator extends BaseValidator {
	private static final Log log = LogFactory.getLog(RegisterObjValidator.class);
		
	public void validateRegisterEntry(RegisterObj r, ValidationContext context) {
		log.debug("validateRegisterEntry() entry");
		MessageContext messages = context.getMessageContext();
		
		validateEmptyString(r.getSalutation(), "salutation", messages);
		validateEmptyString(r.getFullName(), "fullName", messages);
		validateEmptyString(r.getFirstName(), "firstName", messages);
		validateEmptyString(r.getLastName(), "lastName", messages);
		validateEmptyString(r.getDesignation(), "designation", messages);
		validateEmptyString(r.getCompanyName(), "companyName", messages);
		validateEmail(r.getEmail(), "email", messages);
		validateEmail(r.getPaEmail(), "paEmail", false, messages);
		if(validateEmptyString(r.getTelephone(), "telephone", messages))
			if(!PHONE_PATTERN.matcher(r.getTelephone()).matches())
				addMessage("telephone", "invalid.telephone", messages);
		if(validateEmptyString(r.getMobile(), "mobile", messages))
			if(!PHONE_PATTERN.matcher(r.getMobile()).matches())
				addMessage("mobile", "invalid.telephone", messages);
		if(!StringUtils.isBlank(r.getPaTelephone())
				&& !PHONE_PATTERN.matcher(r.getMobile()).matches())
			addMessage("paTelephone", "invalid.telephone", messages);
	}
}
